package com.golddaymicromall.activity.storemanager;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.OrderInfo;
import com.golddaymicromall.bean.StoreTypeInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class StoreEditTypeActivity extends BaseActivity implements View.OnClickListener {

    //控件类
    private TextView mStoreUserName;
    private Spinner mStoreType;
    private LinearLayout mStoreBack;
    private Button mStoreTypeConfirm;
    //参数类
    List<String> listType = null;
    private String mTextValue;
    private String logid;
    private List<OrderInfo> listOrderInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_edit_type);
        initView();
        attemptGetStoreTypeTask();
        attemptGetOrderInfo();
    }

    public void initView() {
        mStoreUserName = (TextView) findViewById(R.id.storeUserName);
        mStoreType = (Spinner) findViewById(R.id.storeType);
        mStoreBack = (LinearLayout) findViewById(R.id.storeType_back);
        mStoreTypeConfirm = (Button) findViewById(R.id.storeEditTypeConfirm);
        mStoreTypeConfirm.setOnClickListener(this);
        mStoreBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.storeType_back:
                finish();
                break;
            case R.id.storeEditTypeConfirm:
                attemptEditTypeTask();
                MyApplication.getInstance().attemptRiZhi(SPUtils.get(getApplicationContext(), "Customerno", "").toString(),
                        SPUtils.get(getApplicationContext(), "Customerno", "").toString() + ",编辑店铺类型，修改店铺");
                break;
        }
    }

    //获取店铺类型数据
    private void attemptGetStoreTypeTask() {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.dictionnary.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("Parentid", "212");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(StoreEditTypeActivity.this, "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(StoreEditTypeActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            String username = SPUtils.get(getApplicationContext(), "Username", "").toString();
                            mStoreUserName.setText(username);
                            List<StoreTypeInfo> listStoreType = JSON.parseArray(result, StoreTypeInfo.class);

                            listType = new ArrayList<String>();
                            for (int i = 0; i < listStoreType.size(); i++) {
                                listType.add(listStoreType.get(i).getName());
                            }
                            // adpater对象
                            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(StoreEditTypeActivity.this, android.R.layout.simple_spinner_dropdown_item, listType);
                            mStoreType.setAdapter(arrayAdapter);
                            mStoreType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view,
                                                           int position, long id) {
                                    Spinner spinner = (Spinner) parent;
                                    for (int i = 0; i < listStoreType.size(); i++) {
                                        if (spinner.getItemAtPosition(position).toString().equals(listStoreType.get(i).getName())) {
                                            mTextValue = listStoreType.get(i).getTextvalue();
                                        }
                                    }
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {
                                    Toast.makeText(getApplicationContext(), "没有改变的处理",
                                            Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });
    }

    //修改店铺类型
    private void attemptEditTypeTask() {
        if (listOrderInfo.size() >= 1) {
            Toast.makeText(StoreEditTypeActivity.this, "店铺级别不能修改!您已进货，请删除订单后修改", Toast.LENGTH_SHORT).show();
            return;
        }
        logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.shop.updategrade");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerId", logid);
        form.put("shopGrade", mTextValue);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (Integer.parseInt(result) == 1) {
                        Toast.makeText(StoreEditTypeActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(StoreEditTypeActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    //获取店铺是否进货
    private void attemptGetOrderInfo() {
        String logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.order.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            listOrderInfo.clear();
                        } else {
                            listOrderInfo = JSON.parseArray(result, OrderInfo.class);
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });
    }

}
